import pygame
import colour

# contents must consist of a sequence of triples (str, on_select, on_choose) where str is what is displayed on the screen
# on_select is what will happen when that option is selected, on_choose is what will happen when that is chosen
# a function may be None
# by default, the last option in the list will be used to exit
# get_display must be a function that can be used to redraw and fetch the base image
class Menu:
    def __init__(self, display, contents, get_display, x=0, y=0, width=320, height=240):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.contents = contents
        self.display = display
        
        self.get_display = get_display
        
        self.selected = 0
        
    def build_menu(self):
        background = self.get_display()
        
        menu_box = pygame.Surface((self.width, self.height))
        # make border
        menu_box.fill(colour.GOLD)
        menu_box.fill(colour.BLACK, (5, 5, self.width-10, self.height-10))
        
        menu_box.set_alpha(208)
            
        #render strings in contents
        font = pygame.font.SysFont(pygame.font.get_default_font(), 18)
        for i in range(len(self.contents)):
            menu_box.blit(font.render(self.contents[i][0], True, colour.LINEN), (20, i*20+20))
        
        # act on the selected option
        menu_box.blit(font.render(self.contents[self.selected][0], True, colour.GOLD), (20, self.selected*20+20))
        if self.contents[self.selected][1] != None:
            self.contents[self.selected][1]()
        
        background.blit(menu_box, (self.x, self.y))
        return background
    
    def show(self):        
        while True:
            self.display.blit(self.build_menu(), (0, 0))
            pygame.display.update()
            
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    exit(0)
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_DOWN:
                        self.selected += 1
                        self.selected %= len(self.contents)
                    elif event.key == pygame.K_UP:
                        self.selected -= 1
                        self.selected %= len(self.contents)
                    elif event.key == pygame.K_RETURN:
                        # last option always assumed to be exit
                        if self.selected == len(self.contents) - 1:
                            return
                        elif self.contents[self.selected][2] != None:
                            self.contents[self.selected][2]()
                            
class Alert:
    def __init__(self, display, text, x=0, y=0, width=240, height=180):
        message_box = pygame.Surface((width, height))
        message_box.fill(colour.GOLD)
        message_box.fill(colour.BLACK, (5, 5, width-10, height-10))        
        message_box.set_alpha(208)
        font = pygame.font.SysFont(pygame.font.get_default_font(), 18)
        message_box.blit(font.render(text, True, colour.LINEN), (20, 20))
        display.blit(message_box, (x, y))
        pygame.display.update()
        
        